<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-data">&nbsp;&nbsp;财务管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-circle-plus">&nbsp;&nbsp;应收帐添加</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div>
      <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
        <el-divider content-position="left">数据添加</el-divider>
        <el-form
            :model="receivablesForm"
            :rules="rules"
            ref="receivablesForm"
            label-width="100px"
            style="margin-right: 25px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="日期:" prop="date">
                <el-date-picker
                    v-model="receivablesForm.date"
                    type="date"
                    format="yyyy 年 MM 月 dd 日"
                    value-format="yyyy-MM-dd"
                    placeholder="选择日期时间">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="收入类型:" prop="incomeType">
                <el-select v-model="receivablesForm.incomeType" placeholder="请选择交易方式">
                  <el-option
                      v-for="item in incomeTypes"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item label="收账-企业名:" prop="description">
            <el-input v-model="receivablesForm.description" placeholder="输入收账-企业名"></el-input>
          </el-form-item>
          <el-form-item label="金额:" prop="amount">
            <el-input v-model="receivablesForm.amount" placeholder="输入金额"></el-input>
          </el-form-item>
          <el-form-item label="交易方式:" prop="tradeMethods">
            <el-select v-model="receivablesForm.tradeMethods" placeholder="请选择交易方式">
              <el-option
                  v-for="item in tradeMethod"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注:" prop="remarks">
            <el-input v-model="receivablesForm.remarks" placeholder="输入备注"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('receivablesForm')" style="color: white">添加</el-button>
            <el-button type="warning" @click="resetForm('receivablesForm')" style="color: white">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      receivablesForm: {
        date: '2023-03-01',
        incomeType: '生产费用',
        description: '应收-百世特',
        amount: '12581',
        tradeMethods: '支付宝',
        remarks: '已交割'
      },
      incomeTypes: [
        {value: "生产费用", label: "生产费用"},
        {value: "外购费用", label: "外购费用"},
        {value: "管理费用", label: "管理费用"},
        {value: "财务费用", label: "财务费用"},
        {value: "营业费用", label: "营业费用"},
      ],
      tradeMethod: [
        {
          value: "支付宝",
          label: "支付宝"
        },
        {
          value: "现金",
          label: "现金"
        },
        {
          value: "微信",
          label: "微信"
        },
        {
          value: "转账",
          label: "转账"
        },
        {
          value: "公司账号",
          label: "公司账号"
        },
      ],
      rules: {
        date: [{required: true, message: "请选择日期", trigger: 'change'}],
        incomeType: [
          {required: true, message: "请输入收入类型,例如XX费", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        description: [
          {required: true, message: "请输入'收账-企业名'", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        amount: [
          {required: true, message: "请输入金额", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        tradeMethod: [
          {required: true, message: "请选择交易方式", trigger: 'change'},
        ],
        remarks: [
          {required: true, message: "请输入备注", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ]
      },
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9080/financeReceivables/add-new';
          console.log('url = ' + url);

          let formData = this.qs.stringify(this.receivablesForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            console.log(responseBody);
            if (responseBody.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加应收帐成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: responseBody.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

</style>